package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmLogistics;
import com.zb.entity.dto.LogisticsDto;
import com.zb.entity.dto.LogisticsList;
import com.zb.entity.dto.LogisticsSelectDto;
import com.zb.exception.RegisterException;
import com.zb.mapper.ZjAdmLogisticsMapper;
import com.zb.service.ZjAdmLogisticsService;
import com.zb.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 后台物流表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-11
 */
@Service
public class ZjAdmLogisticsServiceImpl extends ServiceImpl<ZjAdmLogisticsMapper, ZjAdmLogistics> implements ZjAdmLogisticsService {
    @Autowired
    private ZjAdmLogisticsMapper admLogisticsMapper;

    // 添加或修改物流
    @Override
    public void save(LogisticsDto logisticsDto) throws RegisterException {
        Assert.isTrue(Objects.equals(1, logisticsDto.getType()) || Objects.equals(2, logisticsDto.getType()), "物流类型不合法", logisticsDto.getType());
        if (null != admLogisticsMapper.existByName(logisticsDto.getId(), logisticsDto.getType(), logisticsDto.getName())) {
            throw new RegisterException("物流名称已经存在，不可重复添加或修改！");
        }
        ZjAdmLogistics logistics = new ZjAdmLogistics();
        logistics.setType(logisticsDto.getType());
        logistics.setName(logisticsDto.getName());
        logistics.setCode(logisticsDto.getCode());
        if (null != logisticsDto.getId()) {
            logistics.setId(logisticsDto.getId());
            logistics.setDisabled(false);
            this.updateById(logistics);
            return;
        }
        logistics.setId(IdWorker.getId());
        this.save(logistics);
    }

    // 物流列表接口
    @Override
    public Map<String, Object> getList(LogisticsSelectDto selectDto) {
        QueryWrapper<ZjAdmLogistics> wrapper = new QueryWrapper();
        wrapper.orderByDesc("gmt_update");
        if (null != selectDto.getType()) {
            wrapper.eq("type", selectDto.getType());
        }
        if (StringUtils.isNotBlank(selectDto.getName())) {
            wrapper.like("name", selectDto.getName());
        }
        if (StringUtils.isNotBlank(selectDto.getCode())) {
            wrapper.like("code", selectDto.getCode());
        }
        PageUtil.setPageLimit(selectDto);
        Page<ZjAdmLogistics> page = this.page(new Page<>(selectDto.getPage(), selectDto.getLimit()), wrapper);

        List<LogisticsList> logisticsLists = new ArrayList<>();
        page.getRecords().forEach(t -> logisticsLists.add(LogisticsList.beanToDto(t)));

        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), logisticsLists);
    }

    // 批量启用或禁用物流
    @Override
    public void batchDisableById(List<Long> idList, boolean disable) {
        admLogisticsMapper.updateBatchDisableById(idList, disable);
    }
}
